<?php

namespace App\Http\Controllers;

use App\Models\GoodExample;
use Carbon\Carbon;
use Illuminate\Http\Request;

class GoodExampleController extends Controller
{

    public function __construct(Request $request)
    {
        $user = $request->user();

        if ($user->type != 4 && $user->type != 3) {
            throw new \Exception('权限不足');
        }

        parent::__construct();
    }

    public function index(Request $request)
    {
        $perPage = $request->get('perPage', 10);
        $query = GoodExample::query();
        $user = $request->user();

        if ($user->type == 4) {
            if (!$request->has('org_id')) {
                throw new \Exception('请指定机构信息');
            }

            $query->where('org_id', '=', $request->get('org_id'));
        } else if ($user->type == 3) {
            $query->where('org_id', '=', $user->org_id);
        }

        if ($request->has('keywords')) {
            $query->where('name', 'like', '%' .$request->get('keywords'). '%');
        }

        $info = $query->paginate($perPage)->toArray();

        return $this->returnJSON([
            'count' => $info['total'],
            'rows' => $info['data']
        ]);
    }

    public function store(Request $request)
    {
        $user = $request->user();
        $this->validate($request, [
            'name' => 'required|max:12',
            'avatars' => 'max:255',
            'political_affiliation' => 'max:12',
            'workplace' => 'max:64',
            'org_id' => $user->type == 4 ? 'required' : ''
        ]);

        $node = new GoodExample();
        $data = $request->only([
            'name',
            'avatars',
            'political_affiliation',
            'workplace',
            'status',
            'abstracts',
            'description',
            'publish_time'
        ]);

        $data['status'] = empty($data['status']) ? false : $data['status'];
        $data['avatars'] = isset($data['avatars']) ? $data['avatars'] : '';
        $data['political_affiliation'] = isset($data['political_affiliation']) ? $data['political_affiliation'] : '';
        $data['workplace'] = isset($data['workplace']) ? $data['workplace'] : '';
        $data['abstracts'] = isset($data['abstracts']) ? $data['abstracts'] : '';
        $data['description'] = isset($data['description']) ? $data['description'] : '';

        if (isset($data['status']) && $data['status'] && empty($data['publish_time'])) {
            $data['publish_time'] = (new Carbon())->getTimestamp();
        }

        if ($user->type == 4) {
            $data['org_id'] = $request->get('org_id');
        } else if ($user->type == 3) {
            $data['org_id'] = $user->org_id;
        }

        $node->fill($data);

        if ($node->save()) {
            return $this->returnJSON(null, 0, '新增成功');
        }
        return $this->returnJSON(null, 500, '新建失败');
    }

    public function update(Request $request, $id) {
        $node = GoodExample::findOrFail($id);

        $this->validate($request, [
            'name' => 'required|max:12',
            'avatars' => 'max:255',
            'political_affiliation' => 'max:12',
            'workplace' => 'max:64'
        ]);

        $data = $request->only([
            'name',
            'avatars',
            'political_affiliation',
            'workplace',
            'status',
            'abstracts',
            'description',
            'publish_time'
        ]);

        $data['status'] = empty($data['status']) ? false : $data['status'];
        $data['avatars'] = isset($data['avatars']) ? $data['avatars'] : '';
        $data['political_affiliation'] = isset($data['political_affiliation']) ? $data['political_affiliation'] : '';
        $data['workplace'] = isset($data['workplace']) ? $data['workplace'] : '';
        $data['abstracts'] = isset($data['abstracts']) ? $data['abstracts'] : '';
        $data['description'] = isset($data['description']) ? $data['description'] : '';

        if (isset($data['status']) && $data['status'] == 1 && $node->status != 1 && empty($data['publish_time'])) {
            $data['publish_time'] = (new Carbon())->getTimestamp();
        }

        $node->fill($data);

        if ($node->save()) {
            return $this->returnJSON(null, 0, '保存成功');
        }
        return $this->returnJSON(null, 500, '保存失败');
    }

    public function destroy(Request $request, $id)
    {
        $ids = array_map(function($item) {
            return intval($item);
        }, explode(",", $id));

        if (GoodExample::whereIn('id', $ids)->delete()) {
            return $this->returnJSON($ids, 0, '删除成功');
        } else {
            return $this->returnJSON(null, 1, "删除失败");
        }
    }

    public function online(Request $request, $id)
    {
        $ids = array_map(function($item) {
            return intval($item);
        }, explode(",", $id));
        $result = GoodExample::whereIn('id',  $ids)->update(['status' => 1, 'publish_time' => new Carbon()]);
//    Log::info(Post::where('id', $ids)->toSql());
        if ($result) {
            return $this->returnJSON($ids, 0, '上架成功');
        } else {
            return $this->returnJSON(null, 1, "操作失败");
        }
    }
    public function offline(Request $request, $id)
    {
        $ids = array_map(function($item) {
            return intval($item);
        }, explode(",", $id));
        $result = GoodExample::whereIn('id',  $ids)->update(['status' => 0]);
//    Log::info(Post::where('id', $ids)->toSql());
        if ($result) {
            return $this->returnJSON($ids, 0, '下架成功');
        } else {
            return $this->returnJSON(null, 1, "操作失败");
        }
    }
}
